package in.ac.iitb.cse.nlp.postagger.data;

import java.util.ArrayList;
import java.util.List;


public class TriTableCreator extends TableCreator{
	
	
	String tagSeq[][];
	
	public TriTableCreator()
	{
		tagSeq = new String[3][2];
	}
	
	
	public void updateGram_Conf(String word, String currentTag1)
	{
		String t[] = currentTag1.split("-");
		tagSeq[2][0] = t[0];
		if(t.length>1)
			tagSeq[2][1] = t[1];
		else
			tagSeq[2][1] = null;

		for(int ct=0;ct<2;ct++)
		{
			String currentTag=tagSeq[2][ct];
			if(currentTag == null) continue;
			tagSet.add(currentTag); 
			transitionMatrix.updateCount(currentTag);
			//System.out.println(currentTag+" "+word);
			emissionMatrix.update(currentTag, word); // Emission Count
			
			for(int p1=0;p1<2;p1++)
			{
				if( tagSeq[1][p1] != null )
				transitionMatrix.update(tagSeq[1][p1],currentTag); // Bigram count
			}

			for(int p1=0;p1<2;p1++)
			{
				for(int p2=0;p2<2;p2++)
				{
					if( tagSeq[1][p1] != null && tagSeq[0][p2] != null )
					{
						String p = tagSeq[0][p2]+"|"+tagSeq[1][p1];
						transitionMatrix.updateCount(p);
						transitionMatrix.update(p,currentTag); 
					}
				}
			}			
		}
		
		//List;
		tagSeq[0][0]="";
		for(int i=0;i<2;i++)
			for(int j=0;j<2;j++)
				tagSeq[i][j]=tagSeq[i+1][j];
	}

	public void updateGram_Base(String word, String currentTag)
	{
		tagSet.add(currentTag); //add Tag to set
		transitionMatrix.updateCount(currentTag); // Unigram count
		if( tagSeq[1][0] != null )
		{
			transitionMatrix.update(tagSeq[1][0],currentTag); // Bigram count
			if( tagSeq[0][0] != null ) // Trigram count
			{
				transitionMatrix.updateCount(tagSeq[0][0]+"|"+tagSeq[1][0]);
				transitionMatrix.update(tagSeq[0][0]+"|"+tagSeq[1][0],currentTag); 
			}
		}
		// For updating emission table
		emissionMatrix.update(currentTag, word); // Emission Count
		tagSeq[0][0] = tagSeq[1][0];
		tagSeq[1][0] = currentTag;
	}
	
	public void updateGram(String word, String currentTag)
	{
		if( Config.isAmbigutyResolv == true )
			updateGram_Conf(word, currentTag);
		else
			updateGram_Base(word, currentTag);
	}
	
	public void beginLine()
	{
		String currentTag = "^";
		String word = "^";
		
		updateGram(word, currentTag);
		updateGram(word, currentTag);
	}
	
	public void endLine()
	{
		// only one $ tag at the end
		String currentTag = "$";
		String word = "$";
		updateGram(word, currentTag);
		for(int i=0;i<3;i++)
			for(int j=0;j<2;j++)
				tagSeq[i][j]=null;
	}

	public void processLine(String line)
	{
		String[] split = line.split(WORD_SEPARATOR);
		for (String string : split) {
			String[] strings = string.split(TAG_SEPARATOR); //Dont assume start of the line is start of sentence
			if( strings.length >=2 )
			{
				if( tagSeq[1][0] ==null )
					beginLine();
				
				updateGram(strings[1], strings[0]);
				Config.nTrainWords++;
				
				/* End of Line */
				if (strings[1].equals(END_OF_SENTENCE) || 
						strings[1].equals(ALTERNATE_END_OF_SENTENCE)) {
					endLine();
				}
				
			}
		}
	}
	
	
	public List<String> tagSentence(String sentence)
	{
		String[] split = sentence.split(" ");
		List<String> input = new ArrayList<String>();
		for(String string : split)
			input.add(string);
		HMM vhmm = new BiViterbiHMM();
		vhmm.init(new ArrayList<String>(tagSet),transitionMatrix, emissionMatrix);
		List<String> bestProbTagSeq = vhmm.bestProbTagSeq(input);
		return bestProbTagSeq;
	}	
}
